package com.yanghui.eletask4

open class TerminalData

data class SportData(
    val message: String
) : TerminalData() {

    var heartBeat: Int = 0
    var moveDistance: Int = 0
    var temperature: Double = 0.0
    var stepNum: Int = 0

    init {
        val messageList = message.split(";")
        messageList.forEach {
            try {
                if (it.startsWith("H:")) {
                    this.heartBeat = it.replace("H:", "").trim().toInt()
                } else if (it.startsWith("M:")) {
                    this.moveDistance = it.replace("M:", "").trim().toInt()
                } else if (it.startsWith("T:")) {
                    this.temperature = it.replace("T:", "").trim().toDouble()
                } else if (it.startsWith("S:")) {
                    this.stepNum = it.replace("S:", "").trim().toInt()
                }
            } catch (ignore: NumberFormatException) {}
        }
    }

    constructor(message: String, data: SportData) : this(message) {
        if (this.heartBeat == 0) {
            this.heartBeat = data.heartBeat
        }
        if (this.moveDistance == 0) {
            this.moveDistance = data.moveDistance
        }
        if (this.stepNum == 0) {
            this.stepNum = data.stepNum
        }
        if (this.temperature == 0.0) {
            this.temperature = data.temperature
        }
    }
}

class LogOutData: TerminalData()

data class LoginData(
    val message: String
) : TerminalData() {

    val deviceName: String by lazy {
        if (message.startsWith("L"))
            message.replace("L:", "").trim()
        else
            "识别错误"
    }
}

data class HeartWaveData(
    val message: String
) : TerminalData() {
    val wave: Double by lazy {
        try {
            message.replace("W:", "").trim().toDouble()
        } catch (e: NumberFormatException) {
            5000.0
        }
    }
}